﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using RobozzleModel;

public partial class js_submit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string solution = Request["solution"];
        string userName = HttpContext.Current.User.Identity.Name;
        string levelIdArg = Request["levelId"];
        string error = null;

        int levelId = 0;
        if (levelIdArg == null || !int.TryParse(levelIdArg, out levelId))
        {
            error = "Invalid request.";
        }

        try
        {
            if (error == null)
            {
                new RobozzleService().SubmitSolution(levelId, userName, solution, new RobozzleEntities());
            }
        }
        catch (Exception ex)
        {
            error = ex.ToString();
        }

        Response.Clear();
        Response.ContentType = "text/xml";
        XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        objX.WriteStartDocument();
        if (error == null)
        {
            objX.WriteStartElement("Accepted");
            objX.WriteEndElement();
        }
        else
        {
            objX.WriteElementString("Rejected", error);
        }

        objX.WriteEndDocument();
        objX.Flush();
        objX.Close();
        Response.End();
    }
}
